import SwiftUI

/*
 * 按钮示例
 */
class ButtonExample: FCUIView{
    /*
    * 点击方法
    * view:视图
    * firstTouch:是否第一次触摸
    * firstPoint:第一次触摸的坐标
    * secondTouch:是否第二次触摸
    * secondPoint:第二次触摸的坐标
    * clcks:点击次数
    */
    func onClick(view:FCView, firstTouch:Bool, firstPoint:FCPoint, secondTouch:Bool, secondPoint:FCPoint, clicks:Int) {
        onClickDefault(view:view, firstTouch:firstTouch, firstPoint:firstPoint, secondTouch:secondTouch, secondPoint:secondPoint, clicks: clicks)
        if view.viewName == "Button" {
            print(view.text)
        } else if view.viewName == "Button2"{
            print(view.text)
        } else if view.viewName == "Button3"{
            print(view.text)
        } else if view.viewName == "Button4" {
            print(view.text)
        }
    }

    /*
    * 重绘背景的实现方法
    * view:视图
    * paint:绘图对象
    * clipRect:裁剪区域
    */
    func onPaint(view:FCView, paint:FCPaint, clipRect:FCRect) {
        if view.viewName == "Button4" {
            paint.fillRect(color:"rgb(255,0,0)", left:0, top:0, right:view.size.cx, bottom:view.size.cy);
        }else{
            onPaintDefault(view:view, paint:paint, clipRect:clipRect)
        }
    }
    
    /*
     * 初始化布局
     */
    override func onLoad(){
        let isInit = super.m_isInit;
        super.onLoad();
        if !isInit{
            m_paint.defaultUIStyle = "light"
            let width = Double(self.frame.size.width), height = Double(self.frame.size.height)
            renderFaceCat(name:"buttonexample", paint: m_paint, size: FCSize(cx:width, cy:height))
            m_paint.addPaintEvent(callBack: onPaint)
            m_paint.addClickEvent(callBack: onClick)
            //获取第二页
            let tabPage = findViewByName(name:"TabPage2", views:m_paint.views);
            let button = FCButton()
            button.location = FCPoint(x:100, y:100)
            button.size = FCSize(cx:100, cy:20)
            addViewToParent(view:button, parent:tabPage)
            button.text = "确定"
        }
    }
}
